﻿using D.UtilCore.Dtos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace D.UtilCore.Filters
{
    /// <summary>
    /// 响应结果补充
    /// 执行顺序：1. Action执行完后> OnResultExecuting> 渲染UI视图> OnResultExecuted
    /// 使用场景：Json格式统一处理返回参数
    /// </summary>
    public class ExtResultFilterAttribute : Attribute, IResultFilter
    {

        /// <summary>
        /// 在Action中return后执行
        /// </summary>
        /// <param name="context"></param>

        public void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.Result is JsonResult)
            {
                var result = (JsonResult)context.Result;
                context.Result = new JsonResult(new ApiResult<object>()
                {
                    Data = result.Value
                });
            }
        }

        /// <summary>
        /// 在MVC UI视图执行完成后执行
        /// </summary>
        /// <param name="context"></param>
        public void OnResultExecuted(ResultExecutedContext context)
        {

        }
    }
}
